import 'package:dio/dio.dart';
import 'package:flutter_changdian_football/api/api.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/http_utils.dart';
import 'package:flutter_changdian_football/global.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class ReqMatch {

  /// 比赛记录
  static Future<ApiResponse<dynamic>> userMatchRecords(
    {
      int pageNum = 1
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.userMatchRecords, data: {
        'pageNum': pageNum,
        'pageSize': Global.pageSize
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部已完成的比赛记录
  static Future<ApiResponse<dynamic>> clubMatchList(
    {
      int pageNum = 1,
      String? clubId
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.clubMatchList, data: {
        'pageNum': pageNum,
        'pageSize': Global.pageSize,
        'clubId': clubId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 历史比赛记录详情
  static Future<ApiResponse<dynamic>> historyMatchRecord({
    String? clubId,
    String? matchId
  }) async {
    try {
      var response = await HttpUtils.post(Api.historyMatchRecord, data: {
        "clubId": clubId,
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 俱乐部最近比赛列表 - 返回俱乐部最近的三场比赛
  static Future<ApiResponse<dynamic>> clubRecentMatch({
    required String clubId
  }) async {
    try {
      var response = await HttpUtils.post(Api.clubRecentMatch, data: {
        'clubId': clubId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 创建比赛
  static Future<ApiResponse<dynamic>> createMatch({
    required String clubId,
    required String courtId,
    required String courtName,
    required String siteFees,
    required String playerLimit,
    required String matchStartTime,
    required String matchEndTime,
    required String matchRulesName,
    required String matchRulesValue,
    required bool isPunishment,
    String? remark,
    String? jerseyColor,
    String? opponent,
  }) async {
    try {
      var response = await HttpUtils.post(Api.createMatch, data: {
        "clubId": clubId,
        "courtId": courtId,
        "courtName": courtName,
        "isPunishment": isPunishment,
        "jerseyColor": jerseyColor,
        "matchEndTime": matchEndTime,
        "matchRulesName": matchRulesName,
        "matchRulesValue": matchRulesValue,
        "matchStartTime": matchStartTime,
        "playerLimit": playerLimit,
        "remark": remark,
        "siteFees": siteFees,
        'opponent': opponent
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 查询用户待开始和报名中的比赛数量
  static Future<ApiResponse<dynamic>> getUserMatchNum() async {
    try {
      var response = await HttpUtils.get(Api.getUserMatchNum);
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 获取报名中比赛列表或待开始比赛列表
  static Future<ApiResponse<dynamic>> getUserMatchInfo(
    {
      String? matchType
    }
  ) async {
    try {
      var response = await HttpUtils.post(Api.getUserMatchInfo, data: {
        'matchType': matchType,
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 查看比赛详情
  static Future<ApiResponse<dynamic>> matchDetail({
    required String clubId,
    required String matchId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.matchDetail, data: {
        "clubId": clubId,
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 修改比赛时数据回显接口
  static Future<ApiResponse<dynamic>> matchModifyData({
    required String matchId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.matchModifyData, data: {
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 修改俱乐部比赛
  static Future<ApiResponse<dynamic>> modifyMatch({
    required String clubId,
    required String courtId,
    required String courtName,
    required String siteFees,
    required String playerLimit,
    required String matchStartTime,
    required String matchEndTime,
    required String matchRulesName,
    required String matchRulesValue,
    required bool isPunishment,
    required String matchId,
    String? remark,
    String? jerseyColor,
    String? opponent
  }) async {
    try {
      var response = await HttpUtils.post(Api.modifyMatch, data: {
        "clubId": clubId,
        "courtId": courtId,
        "courtName": courtName,
        "isPunishment": isPunishment,
        "matchEndTime": matchEndTime,
        "matchRulesName": matchRulesName,
        "matchRulesValue": matchRulesValue,
        "matchStartTime": matchStartTime,
        "playerLimit": playerLimit,
        "remark": remark,
        "siteFees": siteFees,
        "matchId": matchId,
        'jerseyColor': jerseyColor,
        'opponent': opponent
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 比赛报名
  static Future<ApiResponse<dynamic>> signUp({
    required String matchId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.signUp, data: {
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 获取比赛场地费和用户余额
  static Future<ApiResponse<dynamic>> matchSinUpFee({
    required String matchId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.matchSinUpFee, data: {
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 缴纳报名费
  static Future<ApiResponse<dynamic>> paySignUpFee({
    required String matchId,
    required String payAmount,
    required String paymentMethod
  }) async {
    try {
      var response = await HttpUtils.post(Api.paySignUpFee, data: {
        "matchId": matchId,
        "payAmount": payAmount,
        "paymentMethod": paymentMethod,
        "paymentType": "APP"
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 取消报名
  static Future<ApiResponse<dynamic>> cancelSignUp({
    required String matchId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.cancelSignUp, data: {
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// 取消比赛
  static Future<ApiResponse<dynamic>> cancelMatch({
    required String matchId,
    required String clubId,
  }) async {
    try {
      var response = await HttpUtils.post(Api.cancelMatch, data: {
        "clubId": clubId,
        "matchId": matchId
      });
      return ApiResponse.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    } finally {
      EasyLoading.dismiss();
    }
  }
  

}